﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MandalaySoft.Common;
using System.Data.SqlClient;
using System.Data;

namespace SIMS.BL
{
    public class MarkingPeriodDB
    {
        MyDatabase _db = new MyDatabase();

        public DataTable GetMarkingPeriodList(int ParentMPID, int SchoolID, Int16 Type, out MyError error)
        {
            DataTable _dt = new DataTable("tblMarkingPeriod");
            SortedList _paramList = new SortedList();
            _paramList.Add("@SchoolID", SchoolID);
            _paramList.Add("@ParentMPID", ParentMPID);
            _paramList.Add("@Type", Type);
            _dt = _db.GetDataTable("sprocMarkingPeriodSelectItemList", CommandType.StoredProcedure, _paramList, out error);
            return _dt;
        }

        public int Delete(int MPID, out MyError error)
        {
            SortedList _paramList = new SortedList();
            _paramList.Add("@MPID", MPID);

            return _db.ExecuteNonSql("sprocMarkingPeriodDeleteSingleItem", CommandType.StoredProcedure, _paramList, out error);
        }

        public MarkingPeriod GetItem(int MPID)
        {
            MarkingPeriod item = new MarkingPeriod();

            SortedList _paraList = new SortedList();
            _paraList.Add("@MPID", MPID);

            MyError _error = new MyError();
            SqlDataReader _reader = _db.GetSqlDataReader("sprocMarkingPeriodSelectSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
            item.Error = _error;

            if (_error.ErrorNo == 0 && _reader.Read())
            {
                item = FillData(_reader, item);
            }

            return item;
        }

        public MarkingPeriod Save(MarkingPeriod item)
        {
            try
            {
                SortedList _paraList = new SortedList();
                _paraList.Add("@MPID", item.MPID);
                _paraList.Add("@ParentMPID", item.ParentMPID);
                _paraList.Add("@SchoolID", item.SchoolID);
                _paraList.Add("@Type", item.Type);
                _paraList.Add("@Title", item.Title);
                _paraList.Add("@ShortName", item.ShortName);
                _paraList.Add("@StartDate", item.StartDate);
                _paraList.Add("@EndDate", item.EndDate);
                _paraList.Add("@IsGraded", item.IsGraded);
                _paraList.Add("@IsExam", item.IsExam);
                _paraList.Add("@IsComment", item.IsComment);
                _paraList.Add("@GradePostStartDate", item.GradePostStartDate);
                _paraList.Add("@GradePostEndDate", item.GradePostEndDate);
                _paraList.Add("@IsActive", item.IsActive);
                _paraList.Add("@CreatedBy", item.CreatedBy);
                _paraList.Add("@ModifiedBy", item.ModifiedBy);

                MyError _error = new MyError();
                Int64 _retValue = _db.Execute("sprocMarkingPeriodInsertUpdateSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
                item.Error = _error;

                if (_retValue > 0)
                {
                    item.MPID = Convert.ToInt32(_retValue);
                    item.Error.ErrorMessage = "Saved successfully!";
                }
                else
                {
                    if (_error.ErrorNo == 0)
                    {
                        item.Error.ErrorNo = 10002;
                        item.Error.ErrorLocation = "MarkingPeriod";
                        item.Error.ErrorMessage = "No row affected.";
                    }
                }

            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10001;
                item.Error.ErrorLocation = "MarkingPeriod";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "MarkingPeriod");
            }

            return item;
        }

        private MarkingPeriod FillData(IDataReader aRow, MarkingPeriod item)
        {
            try
            {
                item = new MarkingPeriod();
                item.MPID = aRow.GetInt32(aRow.GetOrdinal("MPID"));
                item.ParentMPID = aRow.GetInt32(aRow.GetOrdinal("ParentMPID"));
                item.SchoolID = aRow.GetInt32(aRow.GetOrdinal("SchoolID"));
                item.Type = aRow.GetInt16(aRow.GetOrdinal("Type"));
                item.Title = aRow.GetString(aRow.GetOrdinal("Title"));
                item.StartDate = aRow.GetDateTime(aRow.GetOrdinal("StartDate"));
                item.EndDate = aRow.GetDateTime(aRow.GetOrdinal("EndDate"));
                item.IsGraded = aRow.GetBoolean(aRow.GetOrdinal("IsGraded"));
                item.IsExam = aRow.GetBoolean(aRow.GetOrdinal("IsExam"));
                item.IsComment = aRow.GetBoolean(aRow.GetOrdinal("IsComment"));
                if (aRow["ShortName"] != DBNull.Value)
                {
                    item.ShortName = aRow.GetString(aRow.GetOrdinal("ShortName"));
                }
                item.GradePostStartDate = aRow.GetDateTime(aRow.GetOrdinal("GradePostStartDate"));
                item.GradePostEndDate = aRow.GetDateTime(aRow.GetOrdinal("GradePostEndDate"));
                item.IsActive = aRow.GetBoolean(aRow.GetOrdinal("IsActive"));
                item.CreatedBy = aRow.GetInt32(aRow.GetOrdinal("CreatedBy"));
                if (_db.ColumnExists(aRow, "CreatedByName") && aRow["CreatedByName"] != DBNull.Value)
                {
                    item.CreatedByName = aRow.GetString(aRow.GetOrdinal("CreatedByName"));
                }
                item.CreatedDate = aRow.GetDateTime(aRow.GetOrdinal("CreatedDate"));
                item.ModifiedBy = aRow.GetInt32(aRow.GetOrdinal("ModifiedBy"));
                if (_db.ColumnExists(aRow, "ModifiedByName") && aRow["ModifiedByName"] != DBNull.Value)
                {
                    item.ModifiedByName = aRow.GetString(aRow.GetOrdinal("ModifiedByName"));
                }
                item.ModifiedDate = aRow.GetDateTime(aRow.GetOrdinal("ModifiedDate"));
            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10003;
                item.Error.ErrorLocation = "MarkingPeriod";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "MarkingPeriod");
            }

            return item;
        }
    }
}
